package com.jy.wms.service;

import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.WmsLoading;
import com.jy.wms.dao.WmsLoadingMapper;
import com.jy.wms.pojo.WmsShipmentOrder;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: WmsLoadingService.java
 * @Description: 装车单
 * @Author Yjs
 * @Date Wed Jan 31 16:01:23 GMT+08:00 2018
 */

@Service
public class WmsLoadingService extends BaseService<WmsLoadingMapper, WmsLoading> {
    /**
     * 分页查询
     *
     * @param entity 实体
     * @return Page<WmsShipmentOrder>
     */
    @BaseServiceDescribe(moduleName = "出库发运信息", btnName = "分页查询出库发运信息")
    public Page<WmsShipmentOrder> baseSelectPage(WmsShipmentOrder entity) {
        PageHelper.startPage(entity.getPage(), entity.getRows());
        //发运状态3：部分发运 4：全部发运
        List<String> fyztList = new ArrayList<>();
        fyztList.add("3");
        fyztList.add("4");
        //拣货状态1：部分拣货 2：全部拣货
        List<String> jhztList = new ArrayList<>();
        jhztList.add("1");
        jhztList.add("2");
        return (Page<WmsShipmentOrder>) dao.baseSelectPage(entity, fyztList, jhztList);
    }

    /**
     * 新增装车单信息
     *
     * @param entity 实体对象
     * @return
     */
    @BaseServiceDescribe(moduleName = "出库发运信息", btnName = "创建装车单")
    public void addLoading(WmsLoading entity) throws Exception {
        //查询发运订单信息
        List<WmsShipmentOrder> wmsShipmentOrderList = dao.searchFydd(entity.getFybh());
        if (wmsShipmentOrderList.size() > 0) {
            throw new ServiceException("发运编号" + entity.getFybh() + "已创建装车单,不可再次添加");
        } else {
            entity.setId(UUIDUtils.uuid());
            //生成装车单编号
            String zcdbh = searchZcdbh();
            entity.setZcdbh(zcdbh);
            Integer i = dao.addLoading(entity);
            if (i > 0) {
                i = dao.updateZcdbh(entity);
                if (i > 0) {
                    throw new SucessException("创建装车单信息成功");
                } else {
                    throw new ServiceException("更改装车单号失败");
                }
            } else {
                throw new ServiceException("创建装车单信息失败");
            }
        }
    }

    /**
     * 生成装车单编号
     *
     * @return
     */
    public String searchZcdbh() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date time = new Timestamp(System.currentTimeMillis());
        String zcdbhTime = sdf.format(time);
        //模糊查询装车单编号
        String zcdbh = dao.searchZcdbh(zcdbhTime);
        //如果数据库内有装车单编号
        if (!"".equals(zcdbh) && zcdbh != null) {
            String[] maxNum = zcdbh.split("zcdbhTime");
            //取到日期后面的最大值
            long maxZcdbh = Long.parseLong(maxNum[0]);
            //最大值加1
            maxZcdbh += 1;
            zcdbh = zcdbhTime + maxZcdbh;
        } else {
            zcdbh = zcdbhTime + "1";
        }
        return zcdbh;
    }

    /**
     * 承运人模糊查询
     *
     * @param cyrmc
     * @return
     */
    public List<WmsLoading> searchCyr(String cyrmc) {
        return dao.searchCyr(cyrmc);
    }
}
